package com.buba.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPoolConfig;


@Component
public class RedisNmslConfig {

    @Value("${spring.redis.database1}")
    private int database1;

    @Value("${spring.redis.database2}")
    private int database2;

    @Value("${spring.redis.database3}")
    private int database3;

    @Value("${spring.redis.database4}")
    private int database4;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    //@Value("${spring.redis.password}")
    //private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.lettuce.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.lettuce.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.lettuce.pool.min-idle}")
    private int minIdle;

    @Value("${spring.redis.lettuce.pool.max-wait}")
    private int maxWait;

    /**
     * 实时数据的redis
     * @return
     */
    @Bean("realDataRedisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = this.getRedisConnectionFactory(database1); // 建立Redis的连接
        return redisTemplate;
    }

    /**
     * 历史数据的redis
     * @return
     */
    @Bean("hisDataRedisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate2() {
        RedisTemplate<String, Object> redisTemplate = this.getRedisConnectionFactory(database2); // 建立Redis的连接
        return redisTemplate;
    }

    /**
     * 实时报警的redis
     * @return
     */
    @Bean("realAlarmRedisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate3() {
        RedisTemplate<String, Object> redisTemplate = this.getRedisConnectionFactory(database3); // 建立Redis的连接
        return redisTemplate;
    }

    /**
     * 历史报警的redis
     * @return
     */
    @Bean("hisAlarmRedisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate4() {
        RedisTemplate<String, Object> redisTemplate = this.getRedisConnectionFactory(database4); // 建立Redis的连接
        return redisTemplate;
    }

    /**
     * 是负责建立Factory的连接工厂类
     * @param database
     * @return
     */
    public RedisTemplate<String, Object> getRedisConnectionFactory(int database) {
        JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
//        jedisFactory.setHostName(host);
//        jedisFactory.setPort(port);
//        jedisFactory.setPassword(password);
        jedisFactory.setDatabase(database);

        JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        jedisFactory.setPoolConfig(poolConfig);
        jedisFactory.afterPropertiesSet(); // 初始化连接池配置

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(jedisFactory);
        return redisTemplate;
    }
}


